import java.io.*;

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;

public class TestSaver {
   File output = null;
   
   public void setFile (String path){
	   output = new File(path);
   }
   
   public void save (Test test) throws ParserConfigurationException, FileNotFoundException, TransformerException{
	   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       DocumentBuilder builder = factory.newDocumentBuilder();

       // Here instead of parsing an existing document we want to
       // create a new one.
       Document tempDoc = builder.newDocument();
       
       Element testElement = tempDoc.createElement("test");
       Element comparisonDelay = tempDoc.createElement("comparisondelay");
       Element answerDelay = tempDoc.createElement("answerdelay");
       Element tokenBoard = tempDoc.createElement("tokenBoard");
       comparisonDelay.setTextContent("" + test.getComparisonDelay());
       answerDelay.setTextContent("" + test.getAnswerDelay());
       testElement.appendChild(comparisonDelay);
       testElement.appendChild(answerDelay);  
       for (int i = 0; i < test.getNoOfSessions(); i++){
    	   Element session = tempDoc.createElement("session");
    	   Element rewardPhase = tempDoc.createElement("phase");
    	   Element progressBar = tempDoc.createElement("progressBar");
    	   Element sessionName = tempDoc.createElement("sessionName");
    	   Element sessionType = tempDoc.createElement("sessionType");
    	   if (test.getSession(i).isRewardedPhase()){
    		   rewardPhase.setTextContent("true");
    	   } else {
    		   rewardPhase.setTextContent("false");
    	   }
    	   if (test.getSession(i).getProgressBar()){
    		   progressBar.setTextContent("true");
    	   } else {
    		   progressBar.setTextContent("false");
    	   }
    	   sessionName.setTextContent(test.getSession(i).getName());
    	   sessionType.setTextContent(test.getSession(i).getSessionType());
    	   session.appendChild(rewardPhase);
    	   session.appendChild(progressBar);
    	   session.appendChild(sessionName);
    	   session.appendChild(sessionType);
    	   for (int j = 0; j < test.getSession(i).getNoOfProblems(); j++){
    		   Element problem = tempDoc.createElement("problem");
    		   Element sampleImage = tempDoc.createElement("sampleImage");
    		   Element reinforcer = tempDoc.createElement("reinforcer");
    		   Element MTS = tempDoc.createElement("MTS");
    		   sampleImage.setTextContent(test.getSession(i).getProblem(j).getSample());
    		   reinforcer.setTextContent("" + test.getSession(i).getProblem(j).getReinforcer());
    		   if (test.getSession(i).getProblem(j).isMTS()){
        		   MTS.setTextContent("true");
        	   } else {
        		   MTS.setTextContent("false");
        	   }
    		   problem.appendChild(sampleImage);
    		   problem.appendChild(reinforcer);
    		   problem.appendChild(MTS);
    		   for (int k = 0; k < test.getSession(i).getProblem(j).getNoOfComparisons(); k++){
    			   Element comparison = tempDoc.createElement("comparison");
    			   Element image = tempDoc.createElement("image");
    			   if (test.getSession(i).getProblem(j).getComparison(k).getAnswer()){
    				   comparison.setAttribute("answer", "true");
    			   } else {
    				   comparison.setAttribute("answer", "false");
    			   }
    			   image.setTextContent(test.getSession(i).getProblem(j).getComparison(k).getImage());
    			   comparison.appendChild(image);
    			   problem.appendChild(comparison);
    		   }
    		   session.appendChild(problem);
    	   }
    	   testElement.appendChild(session);
       }
       
       //token board
       for (int i = 0; i < test.getTokenBoard().getNoOfTokens(); i++){
    	   Element token = tempDoc.createElement("token");
    	   token.setAttribute("image", test.getTokenBoard().getToken(i).getImage());
    	   tokenBoard.appendChild(token);
       }
       
       testElement.appendChild(tokenBoard);
       tempDoc.appendChild(testElement);


       DOMSource source = new DOMSource(tempDoc);
       PrintStream ps = new PrintStream(output);
       StreamResult result = new StreamResult(ps);

       TransformerFactory transformerFactory = TransformerFactory.newInstance();
       Transformer transformer = transformerFactory.newTransformer();
       transformer.transform(source, result);
   }
   
   public boolean fileIsSet (){
	   if (output == null){
		   return false;
	   } else {
		   return true;
	   }
   }
}
